package com.lb.qqzonepullview;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.Transformation;
import android.widget.ImageView;
import android.widget.ListView;

public class PullListView extends ListView {
    private ImageView mIvHeadBig;
    private int mOriginHeight = 0;// 图片原始宽高
    private int mMaxHeight = 500;// 可滑动到的最大尺寸
    private ImageView mIvHeadSmall;

    private RotateAnimation animation;

    public PullListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inits();
    }

    public PullListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inits();
    }

    public PullListView(Context context) {
        super(context);
        inits();
    }
    
    private void inits() {
        mOriginHeight = getResources().getDimensionPixelSize(R.dimen.head_height);
        
    }
    
    public void setHeadBig(ImageView headBig) {
        this.mIvHeadBig = headBig;
    }

    public void setHeadSmall(ImageView headSmall) {
        this.mIvHeadSmall = headSmall;
    }

    @Override
    protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY,
            int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY,
            boolean isTouchEvent) {
        /**
         * deltaY 滑动过度的位移值 - 向下拉 ;+ 向上滑
         */

        if (deltaY < 0) {// 放大
            int offsetY = mIvHeadBig.getHeight() - deltaY;
            Log.e("lb", "overScrollBy deltaY:" + deltaY + ";mOriginHeight:" + mOriginHeight);
            if (offsetY <= mMaxHeight) {// 放大到图片原始宽高后 不在继续放大
                mIvHeadBig.getLayoutParams().height = mIvHeadBig.getHeight() - deltaY;
                mIvHeadBig.requestLayout();
                if (animation == null) {
                    animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f,
                            Animation.RELATIVE_TO_SELF, 0.5f);
                    animation.setDuration(100);
                    animation.setRepeatCount(100);
                    mIvHeadSmall.startAnimation(animation);
                }
            }

        } else {
            Log.e("lb", "overScrollBy deltaY:" + deltaY);
            // 上拉过渡，不断减小ImageView的高度，deltay是正数，减小高度还是减去
            if (mIvHeadBig.getHeight() > mOriginHeight) {
                mIvHeadBig.getLayoutParams().height =
                        Math.max(mIvHeadBig.getHeight() - deltaY, mOriginHeight);
                // View重新调整宽高
                mIvHeadBig.requestLayout(); // onMeasure-->onLayout
            }
        }
        
        return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY,
                maxOverScrollX, maxOverScrollY, isTouchEvent);
    }
    
    
    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        ViewGroup viewParent = (ViewGroup) mIvHeadBig.getParent();
        int topOffset = viewParent.getTop();
        Log.e("lb", "onScrollChanged t:" + topOffset + ";mIvHeadBig.getHeight():"
                + mIvHeadBig.getHeight());
        if (topOffset < 0 && mIvHeadBig.getHeight() > mOriginHeight) {// 向上滑动 变小 一直变小到原始尺寸后则不在变化
            // 减小ImageView的高度 -- 不能超过图片最原始的高度
            mIvHeadBig.getLayoutParams().height =
                    Math.max(mIvHeadBig.getHeight() + topOffset, mOriginHeight);
            // ImageView所在的容器的高度也要变化：getTop--->0
            viewParent.layout(viewParent.getLeft(), 0, viewParent.getRight(),
                    viewParent.getHeight());
            mIvHeadBig.requestLayout();
        }
        
    }

    /**
     * 监听手指松开
     */
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        // 手指放开时
        if (ev.getAction() == MotionEvent.ACTION_UP) {
            if (mIvHeadBig.getHeight() > mOriginHeight) {
                ResetAnimation animation = new ResetAnimation(mIvHeadBig, mOriginHeight);
                animation.setDuration(300);
                mIvHeadBig.startAnimation(animation);
            }

        }
        return super.onTouchEvent(ev);
    }
    
    public class ResetAnimation extends Animation {
        
        private ImageView mView;

        private int targetHeight;
        // 动画执行前的高度
        private int originalHeight;
        // 高度差
        private int extraHeight;

        public ResetAnimation(ImageView mImageView, int targetHeight) {
            this.mView = mImageView;
            this.targetHeight = targetHeight;
            this.originalHeight = mImageView.getHeight();
            extraHeight = originalHeight - targetHeight;
        }

        /**
         * 动画不断地执行的时候会回调该方法 interpolatedTime：范围是0 0ms-------------->300ms
         * 当前的图片高度--->动画执行之前的高度-高度差*interpolatedTime extraHeight------>0
         */
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            mView.getLayoutParams().height =
                    (int) (originalHeight - extraHeight * interpolatedTime);
            mView.requestLayout();
            super.applyTransformation(interpolatedTime, t);
        }
    }
}
